﻿Imports ATS.Business.Entities
Imports ATS.Data
Imports System.Transactions
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Public Class AssetComponent

#Region "CRUD"
    Public Function CreateAsset(_AssetDetails As MainAssetsTable) As MainAssetsTable
        Return CreateNewAsset(_AssetDetails)
    End Function

    Public Function ReadMainAssetDetails(_searchAsset As String) As MainAssetsTable
        ' Retrieve data.
        Dim dac As New AssetDataAccess()
        Try
            Return dac.GetAsset(_searchAsset)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Public Function GetAllAssets() As List(Of MainAssetsTable)
        ' Retrieve data.
        Dim dac As New AssetDataAccess()
        Try
            Return dac.GetAllAssets()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Public Function UpdateAsset(_AssetDetails As MainAssetsTable) As MainAssetsTable
        Return UpdateAssetDetails(_AssetDetails)
    End Function

    Public Sub DeleteMainAsset(_AssetDetails As MainAssetsTable)
        Dim dac As New AssetDataAccess
        Try
            dac.DeleteAsset(_AssetDetails)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try

    End Sub
#End Region

#Region "Support Procedures"
    Private Function CreateNewAsset(_AssetDetails As MainAssetsTable) As MainAssetsTable
        Dim dac As New AssetDataAccess()
        Try
            _AssetDetails.DateCreated = Now
            _AssetDetails.LastModified = Now
            Return dac.CreateAsset(_AssetDetails)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Throw ex
        End Try
    End Function

    Private Function UpdateAssetDetails(_AssetDetails As MainAssetsTable) As MainAssetsTable
        Using ts As New TransactionScope(TransactionScopeOption.Required)
            Try
                Dim dac As New AssetDataAccess()
                _AssetDetails.DateCreated = Now
                _AssetDetails.LastModified = Now
                'since location is an editable text box, check if location code exist and add if not
                If Not _AssetDetails.LocationCode Is Nothing And dac.ReadLocation(_AssetDetails.LocationCode) Is Nothing Then
                    Dim new_location As New Location
                    With new_location
                        .LocationCode = _AssetDetails.LocationCode
                        .LocationName = _AssetDetails.LocationCode
                        .IsActive = True
                    End With
                    dac.CreateLocation(new_location)
                End If

                'since department is an editable text box, check if location code exist and add if not
                If Not _AssetDetails.DeptCode Is Nothing And dac.GetDepartment(_AssetDetails.DeptCode) Is Nothing Then
                    Dim new_department As New Department
                    With new_department
                        .DepartmentCode = _AssetDetails.DeptCode
                        .DepartmentName = _AssetDetails.DeptCode
                    End With
                    dac.CreateDepartment(new_department)
                End If

                dac.UpdateAssetDetails(_AssetDetails)
                ts.Complete()
                ts.Dispose()
            Catch ex As TransactionAbortedException
                Console.WriteLine("TransactionAbortedException Message: {0}", ex.Message)
                Throw ex
            Catch ex As ApplicationException
                Console.WriteLine("ApplicationException Message: {0}", ex.Message)
                Throw ex
            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Throw ex
            End Try
        End Using
        Return _AssetDetails
    End Function
#End Region
End Class


''--------------------------------------------------------------------------------
'' Layered Architecture Sample: Expense Sample Application
'' Developed by: Serena Yeoh
'' 
'' For updates, please visit http://www.codeplex.com/layersample
''--------------------------------------------------------------------------------
'' THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
'' KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
'' IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'' PARTICULAR PURPOSE.
''--------------------------------------------------------------------------------

'Imports System.Collections.Generic
'Imports System.Diagnostics
'Imports System.Text
'Imports System.Transactions
'Imports System.Reflection
'Imports System.Threading

'Imports ATS.Data
'Imports ATS.Business.Entities
'Imports System.Data

'' NOTE:
'' 
'' Business Components are used to contain the business processing logic of the
'' application. Business components interacts with data access components to
'' store and retrieved data that is required for processing. Business components
'' can also re-validate data that is passed in from the Presentation or Services
'' layer.
''
'' In this sample, you can see that each business method is coded to perform a 
'' specific task. Individually, they appear to be independent from each other 
'' but collectively, they will be used to assembly the business processing flow
'' through the Business Workflow Component.


'''' <summary>
'''' Core business component.
'''' </summary>
'Public Class AssetComponent

'    Public Sub New()
'    End Sub

'    ''' <summary>
'    ''' Submit an Expense.
'    ''' </summary>
'    ''' <param name="expense">An Expense object.</param>
'    Public Function CreateNewAsset(ByVal asset As MainAssetsTable) As MainAssetsTable
'        Console.WriteLine("Saving... ")

'        Using ts As New TransactionScope(TransactionScopeOption.Required)
'            Try
'                asset = CreateAsset(asset)
'                ts.Complete()
'            Catch ex As Exception
'                Console.WriteLine(ex.Message)
'                Throw ex
'            End Try
'        End Using

'        Console.WriteLine("New ExpenseID = " & asset.Assetno.ToString())

'        Return asset
'    End Function

'    ''' <summary>
'    ''' Submit an Expense.
'    ''' </summary>
'    ''' <param name="expense">An Expense object.</param>
'    Public Sub DeleteExistingAsset(ByVal asset As MainAssetsTable)
'        Console.WriteLine("Deleting... ")

'        Using ts As New TransactionScope(TransactionScopeOption.Required)
'            Try
'                DeleteAsset(asset)
'                ts.Complete()
'            Catch ex As Exception
'                Console.WriteLine(ex.Message)
'                Throw ex
'            End Try
'        End Using

'        Console.WriteLine("Asset No = " & asset.Assetno.ToString() & " deleted.")
'    End Sub


'    Public Function UpdateSelectedAsset(ByVal asset As MainAssetsTable) As MainAssetsTable
'        Console.WriteLine("Updating... ")
'        Return UpdateAssetRecord(asset)
'    End Function



'    '''' <summary>
'    '''' Expires an unattended Expense request.
'    '''' </summary>
'    '''' <param name="expense">An Expense object.</param>
'    'Public Function Expire(ByVal expense As Expense) As Expense
'    '    Console.WriteLine("Expiring... ")

'    '    expense.AssignedTo = String.Empty
'    '    expense.Status = ExpenseStatus.Expired
'    '    expense.IsCompleted = True

'    '    Return UpdateExpenseRecord(expense)
'    'End Function

'    '''' <summary>
'    '''' Escalates an unattended Expense request.
'    '''' </summary>
'    '''' <param name="expense">An Expense object.</param>
'    'Public Function Escalate(ByVal expense As Expense) As Expense
'    '    Console.WriteLine("Escalating... ")

'    '    expense.Status = ExpenseStatus.Escalated
'    '    Return UpdateExpenseRecord(expense)
'    'End Function

'    '''' <summary>
'    '''' Approves an Expense request.
'    '''' </summary>
'    '''' <param name="expense">An Expense object.</param>
'    '''' <param name="review">An ExpenseReview object.</param>
'    'Public Function Approve(ByVal expense As Expense, ByVal review As ExpenseReview) As Expense
'    '    Console.WriteLine("Approving...")

'    '    expense.Status = ExpenseStatus.Approved
'    '    expense.IsCompleted = False

'    '    Return UpdateExpenseRecord(expense, review)
'    'End Function

'    '''' <summary>
'    '''' Rejects an Expense.
'    '''' </summary>
'    '''' <param name="expense">An Expense object.</param>
'    '''' <param name="review">An ExpenseReview object.</param>
'    '''' <returns></returns>
'    'Public Function Reject(ByVal expense As Expense, ByVal review As ExpenseReview) As Expense
'    '    Console.WriteLine("Rejecting...")

'    '    expense.AssignedTo = String.Empty
'    '    expense.Status = ExpenseStatus.Rejected
'    '    expense.IsCompleted = True

'    '    Return UpdateExpenseRecord(expense, review)
'    'End Function

'    '''' <summary>
'    '''' Reviews an Expense request.
'    '''' </summary>
'    '''' <param name="expense">An Expense object.</param>
'    '''' <param name="review">An ExpenseReview object.</param>
'    'Public Function Review(ByVal expense As Expense, ByVal review__1 As ExpenseReview) As Expense
'    '    Console.WriteLine("Reviewing...")

'    '    expense.Status = ExpenseStatus.Reviewed
'    '    expense.IsCompleted = False

'    '    Return UpdateExpenseRecord(expense, review__1)
'    'End Function

'    '''' <summary>
'    '''' Disburses an Approved Expense request.
'    '''' </summary>
'    '''' <param name="expense">An Expense object.</param>
'    '''' <param name="review">An ExpenseReview object.</param>
'    'Public Function Disburse(ByVal expense As Expense, ByVal review As ExpenseReview) As Expense
'    '    Console.WriteLine("Disbursing...")

'    '    expense.Status = ExpenseStatus.Disbursed
'    '    expense.AssignedTo = String.Empty
'    '    expense.IsCompleted = True

'    '    Return UpdateExpenseRecord(expense, review)
'    'End Function


'    Private Function UpdateAssetRecord(ByVal asset As MainAssetsTable) As MainAssetsTable
'        Using ts As New TransactionScope(TransactionScopeOption.Required)
'            Try
'                asset = UpdateAsset(asset)
'                ts.Complete()
'            Catch ex As Exception
'                Console.WriteLine(ex.Message)
'                Throw ex
'            End Try
'        End Using

'        Return asset
'    End Function

'    'Private Function UpdateExpenseRecord(ByVal expense As Expense, ByVal review As ExpenseReview) As Expense
'    '    Using ts As New TransactionScope(TransactionScopeOption.Required)
'    '        Try
'    '            expense = UpdateExpense(expense)
'    '            CreateExpenseReview(review)
'    '            LogStatus(expense)
'    '            ts.Complete()
'    '        Catch ex As Exception
'    '            Console.WriteLine(ex.Message)
'    '            Throw ex
'    '        End Try
'    '    End Using

'    '    Return expense
'    'End Function

'    '''' <summary>
'    '''' Returns a list of Expenses that belongs to an employee.
'    '''' </summary>
'    '''' <param name="employeeID">An EmployeeID</param>
'    '''' <returns>A List of Expenses</returns>
'    'Public Function ListExpensesForEmployee(ByVal employeeID As String) As List(Of Expense)
'    '    ' Retrieve data.
'    '    Dim dac As New AssetDataAccess()
'    '    Try
'    '        Return dac.SelectForEmployee(employeeID)
'    '    Catch ex As Exception
'    '        Console.WriteLine(ex.Message)
'    '        Throw ex
'    '    End Try
'    'End Function

'    '''' <summary>
'    '''' Returns a list of Expenses that is assigned to a reviewer.
'    '''' </summary>
'    '''' <param name="reviewerID">A ReviewerID</param>
'    '''' <returns>A List of Expenses</returns>
'    'Public Function ListExpensesForApproval(ByVal reviewerID As String) As List(Of Expense)
'    '    ' Retrieve data.
'    '    Dim dac As New AssetDataAccess()
'    '    Try
'    '        Return dac.SelectForApproval(reviewerID)
'    '    Catch ex As Exception
'    '        Console.WriteLine(ex.Message)
'    '        Throw ex
'    '    End Try
'    'End Function


'    Public Function ListActiveAssets() As MainAssetsTable
'        ' Retrieve data.
'        Dim dac As New AssetDataAccess()
'        Try
'            Return dac.SelectAsset()
'        Catch ex As Exception
'            Console.WriteLine(ex.Message)
'            Throw ex
'        End Try
'    End Function


'    '''' <summary>
'    '''' Retrieves a list of Expense Reviews belonging to an Expense
'    '''' </summary>
'    '''' <param name="expenseID">An ExpenseID.</param>
'    '''' <returns>A List of Expense Reviews.</returns>
'    'Public Function ListExpenseReviews(ByVal expenseID As Long) As List(Of ExpenseReview)
'    '    ' Retrieve data.
'    '    Dim dac As New ExpenseReviewDataAccess()
'    '    Try
'    '        Return dac.SelectByExpenseID(expenseID)
'    '    Catch ex As Exception
'    '        Console.WriteLine(ex.Message)
'    '        Throw ex
'    '    End Try
'    'End Function

'    '''' <summary>
'    '''' Retrieves a list of Expense Logs belonging to an Expense
'    '''' </summary>
'    '''' <param name="expenseID">An ExpenseID.</param>
'    '''' <returns>A List of Expense Logs.</returns>
'    'Public Function ListExpenseLogs(ByVal expenseID As Long) As List(Of ExpenseLog)
'    '    ' Retrieve data.
'    '    Dim dac As New ExpenseLogDataAccess()
'    '    Try
'    '        Return dac.SelectByExpenseID(expenseID)
'    '    Catch ex As Exception
'    '        Console.WriteLine(ex.Message)
'    '        Throw ex
'    '    End Try
'    'End Function

'    ''' <summary>
'    ''' Creates a new Expense record in the database.
'    ''' </summary>
'    ''' <param name="expense">An Expense object.</param>
'    Private Function CreateAsset(ByVal asset As MainAssetsTable) As MainAssetsTable
'        ' Business logic

'        'asset.CreatedBy = Environment.UserName
'        'asset.ModifiedBy = Environment.UserName
'        asset.DateCreated = DateTime.Now
'        asset.LastModified = DateTime.Now

'        Console.WriteLine(asset.ToString())

'        ' Persist data.
'        Dim dac As New AssetDataAccess()
'        Try
'            Return dac.Create(asset)
'        Catch ex As Exception
'            Console.WriteLine(ex.Message)
'            Throw ex
'        End Try
'    End Function

'    ''' <summary>
'    ''' Resets the database to initial state. This should only be done in demos
'    ''' and not in real-life.
'    ''' </summary>
'    Public Sub DeleteAsset(ByVal asset As MainAssetsTable)
'        Using ts As New TransactionScope(TransactionScopeOption.Required)
'            Try
'                Dim dac As New AssetDataAccess()
'                dac.Delete(asset)
'                ts.Complete()
'            Catch ex As Exception
'                Console.WriteLine(ex.Message)
'                Throw ex
'            End Try
'        End Using
'    End Sub


'    ''' <summary>
'    ''' Updates the Expense information into the database.
'    ''' </summary>
'    ''' <param name="expense">An Expense object.</param>
'    Private Function UpdateAsset(ByVal asset As MainAssetsTable) As MainAssetsTable
'        ' Business logic.
'        asset.LastModified = DateTime.Now

'        Console.WriteLine(asset.ToString())

'        ' Persist data.
'        Dim dac As New AssetDataAccess()
'        Try
'            dac.Update(asset)
'        Catch ex As Exception
'            Console.WriteLine(ex.Message)
'            Throw ex
'        End Try

'        Return asset
'    End Function

'    ''' <summary>
'    ''' Resets the database to initial state. This should only be done in demos
'    ''' and not in real-life.
'    ''' </summary>
'    Public Sub Purge()
'        Using ts As New TransactionScope(TransactionScopeOption.Required)
'            Try
'                Dim dac As New AssetDataAccess()
'                dac.Purge()
'                ts.Complete()
'            Catch ex As Exception
'                Console.WriteLine(ex.Message)
'                Throw ex
'            End Try
'        End Using
'    End Sub


'    '''' <summary>
'    '''' Creates a new Expense Review record in the database.
'    '''' </summary>
'    '''' <param name="expense">An Expense Review object.</param>
'    'Private Sub CreateExpenseReview(ByVal review As ExpenseReview)
'    '    ' Business logic.
'    '    review.DateApproved = DateTime.Now

'    '    Console.WriteLine(review.ToString())

'    '    ' Persist data.
'    '    Dim dac As New ExpenseReviewDataAccess()
'    '    Try
'    '        dac.Create(review)
'    '    Catch ex As Exception
'    '        Console.WriteLine(ex.Message)
'    '        Throw ex
'    '    End Try
'    'End Sub


'    '''' <summary>
'    '''' Logs the status of the Expense.
'    '''' </summary>
'    '''' <param name="expense">An Expense object.</param>
'    'Private Sub LogStatus(ByVal expense As Expense)
'    '    Dim log As New ExpenseLog()
'    '    log.ExpenseID = expense.ExpenseID
'    '    log.Status = expense.Status
'    '    log.DateCreated = DateTime.Now

'    '    ' Persist data.
'    '    Dim dac As New ExpenseLogDataAccess()
'    '    Try
'    '        dac.Create(log)
'    '    Catch ex As Exception
'    '        Console.WriteLine(ex.Message)
'    '        Throw ex
'    '    End Try
'    'End Sub

'End Class
